<?php
/**
 * @package pmapi
 * @copyright Copyright (c)2014 Enrico Fagnoni
 * @license GNU General Public License version 3, or later
 *
 * @since 1.0.1
 */

// no direct access
defined('_JEXEC') or die;

jimport('joomla.application.component.helper');

/**
 * see http://docs.joomla.org/Supporting_SEF_URLs_in_your_component
 */
abstract class PmapiHelperRoute
{
    /**
     * find menu itemid for com_pmapi component (if exists) 
     */
    protected static function _findPMapiItemid()
    {
        $app        = JFactory::getApplication();
        $menus      = $app->getMenu('site');
        $component  = JComponentHelper::getComponent('com_pmapi');
        if ( empty($component->id)) return false;     
        $items = $menus->getItems('component_id', $component->id);
        foreach ($items as $item) {
            if (isset($item->query) && isset($item->query['view']) && $item->query['view'] == 'pmapi') return $item->id;
        }
        
        return false;
    }
    
    /**
     * @param   int The route of the content item
     */
    public static function getDataRoute($endpoint,$id=null)
    {
        $result = "index.php?option=com_pmapi&format=data&view=$endpoint";
        if ($id) $result .= "&id=$id";
        if ($itemid = self::_findPMapiItemid()) $result .= "&Itemid=$itemid";
        
        return $result;
    }
}
